package org.sharelife.action;

import org.sharelife.commons.Constants;
import org.sharelife.entity.Message;
import org.sharelife.service.MessageService;
import org.sharelife.util.PageBean;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("all")
public class MessageAction extends ActionSupport {

	private int page = 1;

	private int pageSize = 5;

	private MessageService messageService;

	private PageBean pageBean;

	private Constants constants;

	private Message message;

	private String keyWord;

	// 添加评论
	public String add() throws Exception {
		try {
			messageService.add(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "add_success";
	}

	// 删除评论信息
	public String deleteById() throws Exception {
		try {
			messageService.deleteById(message.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "deleteById_success";
	}

	// 前台根据ID查询留言信息
	public String find_ById() throws Exception {
		try {
			message = messageService.findById(message.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "find_ById_success";
	}

	// 后台分页查询所有评论
	public String findAll() throws Exception {
		try {
			pageBean = messageService.queryForPage(pageSize, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findAll_success";
	}

	// 根据发布时间查询前5条记录
	public String findByDate() throws Exception {
		try {
			message = (Message) messageService.findByDate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findByDate_success";
	}

	// 后台根据ID查询留言信息
	public String findById() throws Exception {
		try {
			message = messageService.findById(message.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findById_success";
	}

	// 根据图片ID分页查询该用户的所有评论
	public String findByPictureId() throws Exception {
		try {
			pageBean = messageService.queryForPageByUserId(message.getUser()
					.getId(), pageSize, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findByPictureId_success";
	}

	// 根据用户ID分页查询该用户的所有评论
	public String findByUserId() throws Exception {
		try {
			pageBean = messageService.queryForPageByUserId(message.getUser()
					.getId(), pageSize, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findByUserId_success";
	}

	// 根据关键字分页查询用户评论信息
	public String getMessagesByKeyWords() throws Exception {
		try {
			pageBean = messageService.queryForPageByKeyWords(keyWord, pageSize,
					page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "getMessagesByKeyWords_success";
	}

	public MessageService getMessageService() {
		return messageService;
	}

	public int getPage() {
		return page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setConstants(Constants constants) {
		this.constants = constants;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Constants getConstants() {
		return constants;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public Message getMessage() {
		return message;
	}
}
